#
#  Copyright 2022 The Open Islands Authors. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

import typing

import pyspark

from pyoi.base_operator_driver import BaseOperatorDriver
from pyoi.session import Session as OI_Session
from pyoi.constant import DataStructure
from pyoi.runtime.types import RunArgs
from pyoi.types import DataIO, ModelIO


class OperatorDriver(BaseOperatorDriver):
    def action(self, args: RunArgs) -> typing.Tuple[typing.List[DataIO], typing.List[ModelIO]]:
        if args.entry_point == "Reader":
            from operators.reader import Reader
            run_object = Reader()
        else:
            raise RuntimeError(f"not support {args.entry_point}")
        return run_object.run(args.job_type, args.params, args.input.data, args.input.model)

    def load_data(self, dataio: DataIO):
        # todo
        pass

    def load_model(self, modelio: ModelIO):
        # todo
        pass

    def save_data(self, src_dataio: DataIO, dest_dataio: DataIO):
        if isinstance(src_dataio.data, pyspark.RDD):
            context = OI_Session.get().create_context(DataStructure.SPARK_RDD)
            context.save_text(schema=src_dataio.schema, rdd=src_dataio.data, data_output=dest_dataio)

    def save_model(self, src_modelio: ModelIO, dest_modelio: ModelIO):
        pass

    def data_to_standard_format(self, data):
        raise NotImplementedError

    def _data_from_standard_format_(self, data):
        raise NotImplementedError

    def model_to_standard_format(self, model):
        raise NotImplementedError

    def _model_from_standard_format_(self, model):
        raise NotImplementedError


if __name__ == "__main__":
    OperatorDriver().start()
